520. Detect Capital
1. Question
We define the usage of capitals in a word to be right when one of the following cases holds:
- All letters in this word are capitals, like
"USA"
. - All letters in this word are not capitals, like
"leetcode"
. - Only the first letter in this word is capital, like
"Google"
.
Given a string word
, return true
if the usage of capitals in it is right.
2. Examples
Example 1:
Input: word = "USA"
Output: true
Example 2:
Input: word = "FlaG"
Output: false
3. Constraints
1 <= word.length <= 100
word
consists of lowercase and uppercase English letters.
4. References
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/detect-capital 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
5. Solutions
class Solution {
public boolean detectCapitalUse(String word) {
if (word.equals(word.toUpperCase()) || word.equals(word.toLowerCase())) {
return true;
}
if (word.length() > 1 && Character.isUpperCase(word.charAt(0))) {
for(int i = 1; i < word.length(); i++) {
if (Character.isUpperCase(word.charAt(i))) {
return false;
}
}
return true;
}
return false;
}
}